#pragma once
#include <iostream>
#include <string>
#include <vector>
#include <ctemplate/template.h>

// #include "oj_model.hpp"
#include "oj_model2.hpp"

namespace ns_view
{
    using namespace ns_model;

    const std::string template_path = "./template_html/";

    class View
    {
    public:
        View(/* args */) = default;
        ~View() = default;

        void AllExpandHtml(const std::vector<struct Question>& all_questions, std::string* html)
        {
            // 1.形成路径
            std::string src_html = template_path + "all_questions.html";
            // 2.形成数据字典
            ctemplate::TemplateDictionary root("all_questions");
            for(const auto& question : all_questions)
            {
                ctemplate::TemplateDictionary* sub = root.AddSectionDictionary("question_list");
                sub->SetValue("id", question.id);
                sub->SetValue("title", question.title);
                sub->SetValue("star", question.star);
            }
            // 3.获取被渲染的html
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);

            // 4. 开始完成渲染功能
            tpl->Expand(html, &root);
        }

        void OneExpandHtml(const struct Question question, std::string* html)
        {
            std::string src_html = template_path + "one_question.html";
            ctemplate::TemplateDictionary root("one_question");
            root.SetValue("id", question.id);
            root.SetValue("title", question.title);
            root.SetValue("star", question.star);
            root.SetValue("desc", question.desc);
            root.SetValue("pre_code", question.header);
            ctemplate::Template* tpl = ctemplate::Template::GetTemplate(src_html, ctemplate::DO_NOT_STRIP);
            tpl->Expand(html, &root);
        }
    };
}
